#!/bin/bash
# vim: ts=2 syn=sh

# 1. Load kernel mods
# 2. Load bootnet/sshd
# 3. Get passwd
# 4. Open devices(md,enclayer) , not mount them 
# 5. make_key; mkswap
# 6. Mount root, cp /etc/nemonico to initram  and do the descriptors 

PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin ; export PATH
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib ; export LD_LIBRARY_PATH

set -eE

_chk_err ()
{
		local count
		echo " ERROR:  6 beeps/seconds then reboot."
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count=$(($count+1))
  		test $count -eq 6 && reboot
  	done
}

trap '_chk_err' ERR

/bin/mount -t proc none /proc 
/bin/mount -n -t sysfs none /sys 
mdev -s
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab

. /etc/scripts/env 
. /etc/scripts/utils.fn 
. /etc/scripts/boot.fn

luks="cryptsetup"; export luks

setlogcons 12

echo "##"
echo "## Basic environment loaded ..."

# /proc/cmdline
_rescue () {
	if [ "$(cat /proc/cmdline|grep -i rescue)" ]; then
		echo ""
		echo " RESCUE MODE CALLED "; sleep 1
		echo ""
		cat /etc/files/console_message
		/bin/bash --rcfile /root/.bashrc.rescue
	fi 
}

# main

# device modules
if ! $(cat /proc/cmdline|grep -qi nokernelmodules ) ; then
	[ -x /load_kernel_modules ] &&  . /load_kernel_modules
	mdev -s
fi

_rescue
_step_one
_open_root
_open_other_mps
_kill_pass_prompt

# cp devs
mdev -s
trap - ERR
mount -o remount,rw,sync /mnt/root
# see /etc/rc.d/rc.S
mkdir -p -m 700 /mnt/root/etc/lvm/backup 
# starting the exodus
mount -o remount,ro /mnt/root
umount /proc
umount /sys
set +eE
echo "##"
echo "## Calling init ..."
exec switch_root /mnt/root /sbin/init $@
